/*
 Copyright (c) [Year] [name of copyright holder]
 [Software Name] is licensed under Mulan PSL v2.
 You can use this software according to the terms and conditions of the Mulan
 PSL v2.
 You may obtain a copy of Mulan PSL v2 at:
          http://license.coscl.org.cn/MulanPSL2
 THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
 KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 See the Mulan PSL v2 for more details.
*/

#include "PasteConfig.hpp"
#include "PasteModule.hpp"

int main() {
    // 设置输出编码为UTF-8,在程序目录下新建.bat批处理文件,
    // 输入以下四行代码并保存,直接运行批处理文件即可
    // @echo off
    // chcp 65001
    // test1.exe
    // pause

    // 初始化配置文件
    PasteConfig cfg = PasteConfig();

    // 从配置文件中获取数据库连接信息
    PasteDB::ConnStruct connStr;
    connStr.host = cfg.GetStrValue("DB_HOST").c_str();
    connStr.user = cfg.GetStrValue("DB_USER").c_str();
    connStr.passwd = cfg.GetStrValue("DB_PASSWD").c_str();
    connStr.db = cfg.GetStrValue("DB_DB").c_str();
    // 从配置文件中获取文件目录和扩展名信息
    std::string dir = cfg.GetStrValue("directory");             // 文件目录
    std::string imgExt = cfg.GetStrValue("imageExtension");     // 图片扩展名
    int baseNum = cfg.GetIntValue("baseNumber");                // 图片基础编号
    int materialID = cfg.GetIntValue("materialID");             // 材质ID
    int categoryID = cfg.GetIntValue("categoryID");             // 类别ID
    std::string categoryName = cfg.GetStrValue("categoryName"); // 图片扩展名

    auto now = std::chrono::system_clock::now() + std::chrono::hours(8);

    std::string timeStr = std::format("{:%Y-%m-%d %H:%M:%S}", now);
    std::cout << "开始时间:" << timeStr << std::endl;
    std::cout << "数据库连接信息->host:" << connStr.host << "  user:" << connStr.user << " passwd:" << connStr.passwd
              << " db:" << connStr.db << std::endl;
    std::cout << "文件目录:" << dir << "  图片扩展名:" << imgExt << "  图片基础编号:" << baseNum << "  材质ID:" << materialID
              << "  类别ID:" << categoryID << "  类别名称:" << categoryName << std::endl
              << std::endl;

    // PasteModule::ProductImgIntegration(connStr, "D:/img/");
    std::cout << "1:遍历[" << dir << "]目录下的图像信息[.txt]文档,手动调整同名[" << imgExt << "]图像并自动添加标签及切割!"
              << std::endl;
    std::cout << "2:遍历[" << dir << "]目录下的图像信息[.txt]文档,自动把同名[" << imgExt << "]图像添加标签及切割!"
              << std::endl;
    std::cout << "3:遍历[" << dir << "]目录下的图像信息[.txt]文档,自动把切割后的[" << imgExt
              << "]图像重命名,并把信息保存到数据表[ImgInfo2]!" << std::endl;
    std::cout << "4:遍历数据表[ImgInfo2]中类别ID为[" << categoryID
              << "]信息,并在[poroduct],[poroduct2]中查找对应信息并加以整合整理" << std::endl;
    std::cout << "5:遍历[" << dir << "]目录下的" << imgExt << "图像,把图像缩小到原来的1/4,并保存到[shrink]目录下!"
              << std::endl;

    int choice; // 用户选择
    std::cin >> choice;
    switch (choice) {
    case 1:
        PasteModule::ImgProcess(dir, imgExt, baseNum, true);
        break;
    case 2:
        PasteModule::ImgProcess(dir, imgExt, baseNum, false);
        break;
    case 3:
        PasteModule::SaveImgInfo(connStr, dir, imgExt, baseNum, categoryID, materialID);
        break;
    case 4:
        PasteModule::ProductImgIntegration(connStr, dir, imgExt, baseNum, categoryID, materialID, categoryName);
        break;
    case 5:
        PasteModule::ImgShrink(dir, imgExt);
        break;
    default:
        std::cout << "输入错误!" << std::endl;
    }
    return 0;
}